package top.infopub.job.sys.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.job.sys.dao.OperatorDao;
import top.infopub.job.sys.service.OperatorService;
import top.infopub.model.sys.Operator;
import top.infopub.util.MD5;

import java.util.List;


@Service
public class OperatorServiceImpl extends GenericServiceImpl<Operator, String> implements OperatorService {

    @Autowired
    private OperatorDao operatorDao;

    @Override
    public GenericDao<Operator, String> getDao() {
        return operatorDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                // 执行逻辑删除
                Operator operator = new Operator();
                operator.setBisDelete(1);
                operator.setId(id);
                operatorDao.updateByIdSelective(operator);
            }
        }
    }

    @Override
    public boolean isUsernameExist(String username) {
        return operatorDao.isUsernameExist(username) > 0;
    }

    @Override
    public void updatePassword(String userName, String password) {
        Operator operator = new Operator();
        // MD5加盐
        operator.setSpassword(MD5.salt(userName, password));
        operator.setSuserName(userName);
        operatorDao.updatePwd(operator);
    }

    @Override
    public int queryByNameAndPw(String userName, String pw) {
        Operator opt = new Operator();
        opt.setSuserName(userName);
        // MD5加盐
        opt.setSpassword(MD5.salt(userName, pw));
        return operatorDao.queryByNameAndPw(opt);
    }

    @Override
    public boolean isMobileExist(String smobile) {
        Operator operator = new Operator();
        operator.setSmobile(smobile);
        operator.setBisDelete(0);
        List<Operator> operatorList = operatorDao.selectByEntityWhere(operator);
        if (operatorList != null && operatorList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isEmailExist(String smail) {
        Operator operator = new Operator();
        operator.setSmail(smail);
        operator.setBisDelete(0);
        List<Operator> operatorList = operatorDao.selectByEntityWhere(operator);
        if (operatorList != null && operatorList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Operator> selectOperatorByGroupIdUser(String groupId, String roleType) {
        return operatorDao.selectOperatorByGroupIdUser(groupId,roleType);
    }

    @Override
    public List<Operator> selectOperatorByGroupIdPost(String groupId, String roleType,String orgId,String deptId) {
        return operatorDao.selectOperatorByGroupIdPost(groupId,roleType,orgId,deptId);
    }

    @Override
    public List<Operator> selectOperatorByGroupIdOrgIdsPost(String groupId, String roleType, List<String> orgIds) {
        return operatorDao.selectOperatorByGroupIdOrgIdsPost(groupId,roleType,orgIds);
    }

    public Operator selectById(String id){
        return operatorDao.selectById(id);
    }
}
